#####################
# run.py - 22 feb 2012 - Hessel van der Molen
# Runs & tests 2 defined agent in the domination game.
#####
# USAGE:
# 
# shell> python run.py [elements]
# [elements] is:
# -r <redagent.py>          (               default: defaultAgent)
# -b <blueagent.py>         (               default: defaultAgent)
# -n <numberOfGamesToPlay>  (integer        default: 1)
# -d <Show/Render_Game>     (true/false     default: true)
# -s <stepPerGame>          (integer        default: 600)
# -t <ThinkTimeAgent>       (float(seconds) default: 0.1 seconds)
# -e <AgentToEvaluatue>     (red/blue       default: red)

# enable to run code in map-structure:
import os, sys, time
path, filename = os.path.split("../Domination-Game/domination")
sys.path.append(path)
defaultAgent = '../Domination-Game/domination/agent.py'


#code to run game
from domination import core, run

#default values
redAgent = defaultAgent
blueAgent = defaultAgent
render = 'true'
iterations = 1
max_steps = 600
think_time = 0.1
evaluate = 'red'

arguments = len(sys.argv) - 1
if (arguments%2 == 1):
    print "ERROR not an even number of arguments given"
    print str(len(sys.argv)) + " " + str(sys.argv)
else:
    # set commands
    for i in range(arguments/2):
        cmd = sys.argv[i*2+1]
        val = sys.argv[i*2+2]

        if (cmd == '-r'): #red agend
            redAgent = val
        elif (cmd == '-b'): #blue agent
            blueAgent = val
        elif (cmd == '-n'): #number of iterations
            iterations = int(val)
        elif (cmd == '-d'): # display/render?        
            render = val
        elif (cmd == '-s'): # number of steps per game
            max_steps = int(val)
        elif (cmd == '-t'): # think time of agent
            think_time = float(val)
        elif (cmd == '-e'): # which agent to evaluate?
            evaluate = val
        else: # not recognised
            print "Incorrect Command: " + cmd
    
    # Define settings
    settings = core.Settings(max_steps=max_steps, think_time=think_time)

    # list for keeping track of all scores
    score = []
    steps = []
    
    # Run game(s)
    start = time.time()
    for a in range(iterations):
        
        #run game
        print('running: #' + str(a+1))
        if (render == 'true'):
            game = core.Game(redAgent, blueAgent, record=True, rendered=True, settings=settings)
        else: 
            game = core.Game(redAgent, blueAgent, record=False, rendered=False, settings=settings)
        game.run()
            
        #evaluate score & swap agents
        #swapping is done to make sure there is no advantage in team-color
        if (evaluate == 'red'): 
            score.append(game.score_red)
            evaluate = 'blue'
        else:
            score.append(game.score_blue)
            evaluate = 'red'
        
        #number of game-steps, the game last
        steps.append(game.step)

        # switch agents (blue = red & red = blue)
        redAgent, blueAgent = blueAgent, redAgent

    end = time.time()
    duration = str(int(end-start))
    print('the duration was: '+duration+' seconds')

    # if multiple games are played store score results
    if (len(score) > 1):
        import datetime
        now = datetime.datetime.now()
        filename = str(now.year)
        filename += "_" + str(now.month)
        filename += "_" + str(now.day)
        filename += "_" + str(now.hour)
        filename += "_" + str(now.minute) 
        if (evaluate == 'blue'): #evaluate switches, but game not! (check above loop)
            filename += "_" + game.red.fullname() + ".txt"
        else:
            filename += "_" + game.blue.fullname() + ".txt"
        file = open(filename, "w")
        file.write(str(score))
        file.write("\n")
        file.write(str(steps))
        file.close()
